//---------------------------------------------------------------------------


#pragma hdrstop

#include "Edv.h"
#include "vcl.h"
#include "Math.h"
#include "Math.hpp"
#include <Qt.hpp>
#include <StrUtils.hpp>

//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
AnsiString BoolToAS(bool b)
{
  if (b) return "1";
  else return "0";
}
//---------------------------------------------------------------------------
bool ASToBool(AnsiString s)
{
  if (s == "0") return 0;
  else return 1;
}
//-- Geometric averange of number of points ---------------------------------
void GeomAvr(short int num, TMasCoor *MasX,  TMasCoor *MasY, double &AX, double &AY)
{
  AX = *MasX[0]; AY = *MasY[0];
  for(int i=1; i < num; i++) {
    AX = (AX + *MasX[i])/2;
    AY = (AY + *MasY[i])/2;
  }
  return;
}
//- mantissa of double -------------------------------------------------------
double Frac(const double x)
{ return x - int(x); }

//- factorial ----------------------------------------------------------------
long int factorial(int x)
 {
   int result = 1;
   for(int i = 2; i <= x; i++)
       result *= i;
   return result;
 }
//---------------------------------------------------------------------------
double Fraction(double x)
{
  return x-floor(x);
}
//---------------------------------------------------------------------------
double sqr(double x)
{
  return x*x;
}
//---------------------------------------------------------------------------
double eabs(double x)
{
  return (x < 0)?-x:x;
}
//---------------------------------------------------------------------------
AnsiString GetPath()
{
  AnsiString S = Application->ExeName;
  int l = S.Length();
  int i = l;
  while (S[i] != '\\') i--;
    return S.Delete(i + 1, l-i);
  return S;
}
//-- to get SubString in the string like this : <s><kelias><ilgis>----------
AnsiString GetBrParam(AnsiString Str, int i)
{
  int k1, k2;
  i--;
  for(int j = 0; j < i; j++) {
    k1 = Str.Pos('>');
    k2 = Str.Length();
    Str = Str.SubString(k1 + 1, k2 - k1);
  }
  k2 = Str.Pos('>');
  Str = Str.SubString(2, k2 - 2);
  return Str;
}
//-- find another pair of bracket position increasing -----------------------
int BrackPairI(int p, AnsiString Str, char b1, char b2 )
{
  int L = Str.Length();
  int s1 = p + 1;
  int s2 = p;
  while ((s1 != s2) && (p < L)) {
    p++;
    if (Str[p] == b1) s1++;
    else if (Str[p] == b2) s2++;
  }
  return (Str[p] == b2)?p:0;
}
//-- find another pair of bracket position decrasing ------------------------
int BrackPairD(int p, AnsiString Str, char b1, char b2 )
{
  int s1 = p - 1;
  int s2 = p;
  while ((s1 != s2) && (p > 1)) {
    p--;
    if (Str[p] == b2) s1--;
    else if (Str[p] == b1) s2--;
  }
  return (Str[p] == b1)?p:0;
}
//----------------------------------------------------------------------------
AnsiString ChangeExt(AnsiString FileName, AnsiString NewExt)
{
  return FileName.SubString(1, FileName.Length()-3) + NewExt;
}
//----------------------------------------------------------------------------
AnsiString NameFromPath(AnsiString FilePath)
{
  int i = FilePath.Length();
  int j = i;
  while ((FilePath[i] != Key_Slash) && (FilePath[i] != Key_Backslash) && (i > 1)) i--;
  return FilePath.SubString(i+1, j-i-4);
}
//----------------------------------------------------------------------------
double Power(double x, double n) {
  int k = 1;
  if (n == 1) return x;
  if (n < 0) {
    x = 1/x;
    n = -n;
  }
  if (x < 0) {
    x = -x;
    k = -1;
    if (Frac(n) == 0) {
      if ((int)n%2 == 0) k = 1;
      else k = -1;
    }
  }
  if (n > 1) {
    double fn = Frac(n);
    if (IsZero(fn)) return k*powl(x, n);
    else {
      double s = 1/fn;
      if (x < 0) {
        if (IsZero(Frac(s))) {
          if (int(s)%2 == 0) return 0;
          else return k*Power(x, floor(n))*Power(x, fn);
        } else return 0;
      } else return k*Power(x, floor(n))*Power(x, fn);
    }
  } else return k*powl(x, n);
}
//----------------------------------------------------------------------------
AnsiString RemoveDoubleBracks(AnsiString S, char b1, char b2)
{
  int i = S.Length();
  while (i != 1) {
    while (S[i] != b1) {
      i--;
      if (i == 1) return S;
    }
    if (S[i-1] == b1) {
      int j = BrackPairI(i, S, b1, b2);
      if (BrackPairI(i-1, S, b1, b2) - j == 1) {
        S = S.Delete(i, 1);
        S = S.Delete(j-1, 1);
      }
    }
    i--;
  }
  return S;
}
//- inserts multiplication mark where it's needed ----------------------------
AnsiString ImplyMultiplication(AnsiString S)
{
  TCharSet Before;
  Before << ',' << '-' << '+' << '/' << '*' << '!' << '^' << '<' << '$' << '%' << '0' << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9' << ';';
  TCharSet EBracks;
  EBracks << ')' << ']' << '}';
  TCharSet BBracks;
  BBracks << '(' << '[' << '{';
  int i = S.Length();
  int L = i;
  while (i >= 1) {
    if ((S[i] >= '0') && (S[i] <= '9') || (S[i] == 'x')) {
      if (i < L)
        if (!(Before.Contains(S[i+1]) || EBracks.Contains(S[i+1])))
          S = S.Insert("*", i+1);
      if (i > 1)
        if (!(Before.Contains(S[i-1]) || BBracks.Contains(S[i-1]))) {
          S = S.Insert("*", i);
          i--;
        }
    }
    i--;
  }
  // multiply between bracks
  S = AnsiReplaceStr(S, ")(", ")*(");
  S = AnsiReplaceStr(S, "}{", "}*{");
  S = AnsiReplaceStr(S, "][", "]*[");
  return S;
}
//- inserts 2 before squere mark if needed -----------------------------------
AnsiString ImplySqrt(AnsiString S) {
  int i = S.Length() - 1;
  while (i >= 1) {
    if (S[i] == '<') {
      if (i == 1) S = S.Insert("2", 1);
      else if (!((S[i-1] >= '0') && (S[i-1] <= '9')))
             S = S.Insert("2", i);
    }
    i--;
  }
  return S;
}
//-- checks if there is any error in bracking --------------------------------
bool CheckBracks(AnsiString S, char b1, char b2) {
  int L = S.Length();
  int i = 0;
  int Count1 = 0;
  int Count2 = 0;
  for(i = 1; i <= L; i++) {
    if (S[i] == b1) Count1++;
    else if (S[i] == b2) Count2++;
  }
  return !(Count1 == Count2);
}
//- trows message if threre is an error in the string -----------------------
bool ExpressionError(AnsiString S)
{
  if (CheckBracks(S, '(', ')') || CheckBracks(S, '[', ']') || CheckBracks(S, '{', '}')) {
    ShowMessage("Skliaudimo klaida!");
    return true;
  } return false;
}

